import {cbs, phoneReg, pwdReg} from '../../config/settings.js';
import md5 from 'js-md5';

module.exports = {
    name: 'login',
    data() {
        return {
            winSize: {
                width: '',
                height: ''
            },

            formOffset: {
                position: 'absolute',
                left: '',
                top: ''
            },

            login_actions: {
                disabled: false
            },

            data: {
                account: '',
                password: ''
            },

            rule_data: {
                account: [{
                    validator:(rule, value, callback)=>{
                        if (value === '') {
                            callback(new Error('请输入手机号'));
                        } else if(!phoneReg.test(value)){
                            callback(new Error('请输入有效号码'));
                        }else{
                            callback();
                        }
                    },
                    trigger: 'blur'
                }],
                password: [{
                    validator:(rule, value, callback)=>{
                        if (value === '') {
                            callback(new Error('请输入密码'));
                        } else {
                            if(!(pwdReg.test(value))){
                                callback(new Error('密码长度需在6-16位之间，同时包含数字和字母'));
                            }else{
                                callback();
                            }

                        }
                    },
                    trigger: 'blur'
                }]
            }
        }
    },
    methods: {
        setSize() {
            this.winSize.width = $(window).width() + "px";
            this.winSize.height = $(window).height() + "px";

            this.formOffset.left = (parseInt(this.winSize.width) / 2 - 175) + 'px';
            this.formOffset.top = (parseInt(this.winSize.height) / 2 - 178) + 'px';
        },
        /**
         * 登录
         */
        onLogin(ref,type) {
            this.$refs[ref].validate((valid) => {
                if (valid) {
                    this.login_actions.disabled = true;
                    let pwd = md5(this.data.password)

                    let data = this.$$formData({
                        account: this.data.account,
                        password: pwd
                    })

                    this.$$api_user_login(data, res => {
                        if(res.event === 0) {
                            this.$store.dispatch('update_userinfo', {
                                userinfo: res.obj
                            }).then((data) => {

                                if(res.obj.status === 0) {
                                    this.$router.push('/index/home')
                                    this.hasPayPwd();
                                }else {
                                    // 认证审核不通过
                                    this.$router.push('/authen')
                                }
                            });
                        }else if(res.event === 1) {
                            cbs.submitError.call(this, `登录失败, 用户不存在`);
                            this.login_actions.disabled = false;
                        }else {
                            this.login_actions.disabled = false;
                            cbs.submitError.call(this, `登录失败, ${res.msg}`);
                        }
                    }, {
                        errFn: () => {
                            this.login_actions.disabled = false;
                        },
                        tokenFlag: true
                    });
                    this.login_actions.disabled = false;
                }
            });
        },
        /**
         * 是否设置支付密码
         */
        hasPayPwd() {
            this.$$api_manager_hasPayPwd(null, res => {
                if(res.event === 0) {
                    this.changePayStatus(res.obj);
                }else {
                    cbs.submitError.call(`获取支付设置失败, ${res.msg}`);
                }
            })
        },
        /**
         * 改变支付密码设置状态
         */
        changePayStatus(status) {
            let userinfo = this.$store.state.user.userinfo;
            Object.assign(userinfo, {hasPay: status});

            this.$store.dispatch('update_userinfo', {
                userinfo: userinfo
            })
        },
        /**
         * 跳转至忘记密码
         */
        onReminder() {
            this.$router.push('/reminder');
        },
        /**
         * 跳转至注册
         */
        onRegister(ref) {
            this.$router.push('/register');
        },
    },
    created() {
        this.setSize();
        $(window).resize(() => {
            this.setSize();
        });
    }
}